<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryParams" size="small" :inline="true" >
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="申报单号" prop="id" v-show="showSearch">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入申报单号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="area">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 240px" @change="handleQuery">
          <el-option label="待指派" value="待指派"></el-option>
          <el-option label="待接收" value="待接收"></el-option>
          <el-option label="待维修" value="待维修"></el-option>
          <el-option label="维修中" value="维修中"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="住户编号" prop="customerId" v-show="showSearch">-->
<!--        <el-input-->
<!--          v-model="queryParams.customerId"-->
<!--          placeholder="请输入住户编号"-->
<!--          clearable-->
<!--          style="width: 240px"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
      <el-table v-loading="loading" :data="rowList" >
        <!--      <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column label="申报单号" prop="id" width="120" align="center" fixed="left"/>
        <el-table-column label="申报用户" prop="createBy" width="120" align="center"/>
        <!--      <el-table-column label="住户名称" prop="customerName" width="120" align="center"/>-->
        <el-table-column label="报修园区" prop="area" width="120" align="center"/>
        <el-table-column label="申报内容" prop="content" width="200" align="center"/>
        <el-table-column label="上传照片" prop="image" align="center" :width="imageColWidth">
          <template v-slot:default="scope">
            <image-preview v-for="(item,index) in scope.row.images"
                           style="width: 100px; height: 100px; margin-right: 15px"
                           :src="item">
            </image-preview>

          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" width="120" align="center"/>
        <el-table-column label="指派维修人" prop="keeperName" width="120" align="center"/>
        <el-table-column label="维修内容" prop="keepContent" width="120" align="center"/>
        <el-table-column label="维修照片" prop="keepImage" :width="contentColWidth" align="center">
          <template v-slot:default="scope">
            <image-preview v-for="(item,index) in scope.row.keepImages"
                           style="width: 100px; height: 100px; margin-right: 15px"
                           :src="item">
            </image-preview>
          </template>
        </el-table-column>
        <el-table-column label="具体地址" prop="location" width="150" align="center"/>
        <el-table-column label="设备出库申请" prop="extra" width="120" align="center"/>
        <el-table-column label="评价建议" prop="comment" width="120" align="center"/>
        <!--      <el-table-column label="预约时间" align="center" prop="bookTime" width="180">-->
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.bookTime) }}</span>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column label="修改人" prop="updateBy" width="120" align="center"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" align="center" prop="createTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="140">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >出库申请</el-button>
            <el-button  size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="住户id" prop="customerId">-->
<!--              <el-input  v-model="form.customerId" placeholder="请输入住户id" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="状态" prop="status">-->
<!--              <el-select v-model="form.status"  clearable placeholder="请选择状态">-->
<!--                <el-option label="待指派" value="待指派"></el-option>-->
<!--                <el-option label="待接收" value="待接收"></el-option>-->
<!--                <el-option label="待维修" value="待维修"></el-option>-->
<!--                <el-option label="维修中" value="维修中"></el-option>-->
<!--                <el-option label="已完成" value="已完成"></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-form-item label="预约时间" prop="bookTime">-->
<!--          <el-date-picker v-model="form.bookTime" type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"-->
<!--                          :style="{width: '100%'}" placeholder="请选择预约时间" clearable></el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="具体地址" prop="location">-->
<!--          <el-input  v-model="form.location" placeholder="请输入住户具体地址" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="申报内容" prop="content">-->
<!--          <el-input type="textarea" v-model="form.content" placeholder="请输入申报内容" />-->
<!--        </el-form-item>-->
        <el-form-item label="设备出库申请" prop="extra">
          <el-input type="textarea" v-model="form.extra" placeholder="请输入设备出库申请" />
        </el-form-item>
<!--        <el-form-item label="评价建议" prop="remark">-->
<!--          <el-input type="textarea" v-model="form.comment" placeholder="请输入评价建议" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { list, get, del, add, update } from "@/api/device/report";

export default {
  name: "Report",
  dicts: ['user_area'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      rowList: [],
      imageColWidth: 150,
      contentColWidth: 150,
      commentColWidth: 150,
      extraColWidth: 150,
      locationColWidth: 150,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: "",
        customerId: "",
        area:'',
      },
      // 表单参数
      form: {
        customerId :"",
        content :"",
        status :"",
        bookTime :"",
        comment :"",
        extra :"",
        remark :"",
        location :""
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.rowList = response.rows;
        let maxWith = 1;
        let maxContentWidth = 1;
        for (var i = 0; i < response.rows.length; i++) {
          response.rows[i].images = [];
          response.rows[i].keepImages = [];
          if (response.rows[i].imageUrl != null && response.rows[i].imageUrl != undefined) {
            response.rows[i].images = response.rows[i].imageUrl.split(",");
          }
          if (response.rows[i].keepImageUrl) {
            response.rows[i].keepImages = response.rows[i].keepImageUrl.split(",");
          }
          if (response.rows[i].images.length > maxWith) {
            maxWith = response.rows[i].images.length;
          }
          if (response.rows[i].keepImages.length > maxContentWidth) {
            maxContentWidth = response.rows[i].keepImages.length;
          }
        }
        this.contentColWidth =  maxContentWidth * 150
        this.imageColWidth = this.imageColWidth > maxWith * 150 ? this.imageColWidth : maxWith * 150;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "住户申报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改申报";
      });
    },
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handleDetail":
          this.handleDetail(row);
          break;
        case "handleDetail1":
          this.handleDetail1(row);
          break;
        default:
          break;
      }
    },


    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDetail: function(row) {
      const id = row.id;
      this.$router.push("/device/keep/report/detail/"+id);
    },
    handleDetail1: function(row) {
      const id = row.id;
      this.$router.push("/device/keep/record/index/"+id);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.id || this.ids;
      this.$modal.confirm('请确认是否删除该记录').then(function() {
        return del(roleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
